﻿using System.Collections;
using UnityEngine;

namespace VCity
{
    [ExecuteInEditMode]
    public class AdsEditHelper : MonoBehaviour
    {
#if UNITY_EDITOR
        public AdsBoardManager adsManager;

        [Space]
        public bool replace;
        public string replaceDistrict;
        public AdsBoard replacePrefab;

        // Update is called once per frame
        void Update()
        {
            if (replace)
            {
                replace = false;

                var g = adsManager.adsGroups.Find(g => g.name == replaceDistrict);
                if (g.name == replaceDistrict)
                {
                    for (int i = 0, length = g.boards.Count; i < length; i++)
                    {
                        var board = g.boards[i];
                        var tr = board.transform;
                        var boardNew = Instantiate(replacePrefab, tr.position, tr.rotation, tr.parent);
                        boardNew.name = board.name;

                        g.boards[i] = boardNew;

                        UnityEditor.EditorApplication.delayCall += () =>
                        {
                            DestroyImmediate(board);
                        };
                    }
                }
            }
        }
#endif
    }
}